package com.wsc.frontier;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpHost;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.wsc.crawler.utils.*;
import com.wsc.frontier.database.DataBaseDetails;

/**
 *
 * @author N V Reddy
 */
public class FrontierConfig {
	
   private String url,dbname,user,password,driver,path,received_dir;
   private Logger log=Logger.getLogger(FrontierConfig.class.getName());
    
    public void getDetails(){
    	url=DataBaseDetails.getJdbcUrl();
    	dbname=DataBaseDetails.getDatabaseName();
    	user=DataBaseDetails.getDatabaseUser();
    	password=DataBaseDetails.getDatabasePassword();
    	driver=DataBaseDetails.getDatabaseDriver();
    	path=FrontierDetails.getMysqlPath();
    	received_dir=FrontierDetails.getReceivedURLsDir();
    }
    
    //check mysql avalibility and startup
    public boolean mysqlStartup() throws IOException{
        boolean status = false;
        HttpHost mysqlhost = new HttpHost("localhost",3306);
        
        log.info("Checking whether mysql port is open or not.");
        if(CheckHost.isPortOpen(mysqlhost)){
        	log.info("Looks like mysql port "+mysqlhost.getPort()+" is Open.");
        }
        else{
        try{
        
        	log.info("Looks like mysql is not running");
        	log.info("Trying to Start mysql...");
        	(Runtime.getRuntime()).exec ("/bin/systemctl start  mysqld.service"); 
        	
        }
        catch(Exception ee){
        	log.info("mysql not started: ",ee);
        	return false;
        }
        log.info("mysql is started and checking the connection");
        }
        status = mysqlCheckup();
        return status;
   }
    
    //checking required database
    public boolean mysqlDatabase() throws IOException{
        //checking required database;
        log.info("looking for required database. ");
        Connection con;
        try{
            Class.forName(driver);
            con=(Connection) DriverManager.getConnection(url+dbname,user,password);
            log.info("required database found. ");
            return true;
        }
        catch(ClassNotFoundException e){
            log.info("mysql is started but required database not found. ");
            log.info("the following error ocurred during database connection. ",e);
        }catch(SQLException e){
        	  log.info("mysql is started but required database not found. ");
              log.info("the following error ocurred during database connection. ",e);
        }
        
        return false;
    }
    
    //previous parased urls and received urls handling
    public void cleanPreviousData(){
        /*cleaning parsed urls data
        File directory = new File("C:\\test\\frontier\\parsedurls\\");      
        File[] list_of_files = directory.listFiles();  
        for (int j = 0; j < list_of_files.length; j++){
            if(list_of_files[j].isDirectory()){
                continue;
            }
            if(list_of_files[j].isFile()){
                File path=list_of_files[j];
                cleanParsedURLs(path);
                path.delete();
            }
        }*/
        
        //cleaning received urls data
        File directory = new File(received_dir);
        File[] list_of_files = directory.listFiles();  
        for (int j = 0; j < list_of_files.length; j++){
            if(list_of_files[j].isDirectory()){
                continue;
            }
            if(list_of_files[j].isFile()){
                File path=list_of_files[j];
                cleanReceivedURLs(path);
                path.delete();
            }
        }
    }
  
        
    
    //parsed urls cleaning
    public void cleanParsedURLs(File filename){
        //clean the parsedurls file
    	try {
	    	String url_name;
	    	Connection con;
			Class.forName(driver);
		  	con=DriverManager.getConnection(url+dbname,user,password);
		  	Statement stmt=null;
		  	
		  	
		  	
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(filename);
	        doc.getDocumentElement().normalize();
		        
	        NodeList nList = doc.getElementsByTagName("url");
	        for (int temp = 0; temp < nList.getLength(); temp++) {
	        	Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
					url_name = eElement.getElementsByTagName("name").item(0).getTextContent();
		            String sql = "DELETE FROM `urllist` WHERE url=\'"+url_name+"\';";
		            stmt =  con.createStatement();
		            stmt.executeUpdate(sql);

                }//end of if clause
		    }
	
	            	
	    }
	    catch (SAXParseException err) {
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());

	    }
	    catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();

	    }
	    catch (Throwable t) {
	        t.printStackTrace ();
	    }

        
    }
    
    //received urls cleaning
    public void cleanReceivedURLs(File filename){
        //clean the received urls file
    	try {
	    	String url_name,url_ip;
	    	Connection con;
			Class.forName(driver);
		  	con=DriverManager.getConnection(url+dbname,user,password);
		  	
		  	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(filename);
	        doc.getDocumentElement().normalize();
		        
	        NodeList nList = doc.getElementsByTagName("url");
	        for (int temp = 0; temp < nList.getLength(); temp++) {
	        	Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
					url_name = eElement.getElementsByTagName("name").item(0).getTextContent();
                    url_ip = eElement.getElementsByTagName("ip").item(0).getTextContent();
                    //database insertion
                    
                    PreparedStatement pst = con.prepareStatement("insert into `urllist` (`url`,`ip`)"+ " values(?,?);");
                    pst.setString(1, url_name);
                    pst.setString(2, url_ip);
                    pst.executeUpdate();
                    System.out.println("insertion successful");

                }//end of if clause
		    }
	    }
    	catch (SAXParseException err) {
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());

	        }catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();

	        }catch (Throwable t) {
	        t.printStackTrace ();
	        }
	}
    
    //mysql avalibility
    public boolean mysqlCheckup() throws IOException{
        log.info("checking the connection");
        Connection con = null;
        try{
            Class.forName(driver);
            con=(Connection) DriverManager.getConnection(url,user,password);//keep the user name and password in another file
            log.info("connection can be established successfully. ");
            return true;
        }
        catch(ClassNotFoundException e){
        	 log.info("connection can't be established. ");
             log.info("the following error ocurred during connection. "+e+" ");
        }
        catch(SQLException e){
        	 log.info("connection can't be established. ");
             log.info("the following error ocurred during connection. "+e+" ");
        }finally{
        	if(con != null)
				try {
					con.close();
				} catch (SQLException e) {
					log.error("Error occured while closing mysql connection", e);
				}
        }
           
            return false;
        }
       
    }
 
